package com.mail.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mail.common.DataResult;
import com.mail.mapper.EmailTemplatesMapper;
import com.mail.mapper.MailMapper;
import com.mail.pojo.EmailTemplates;
import com.mail.pojo.Mail;
import com.mail.service.EmailTemplatesService;
import com.mail.service.MailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class MailServiceImpl extends ServiceImpl<MailMapper, Mail> implements MailService {

    @Resource
    private MailMapper mailMapper;

    @Override
    public DataResult queryTimedMail(Map<String, Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));
        String teamIdString=(String) map.get("teamId");
        String userId=(String) map.get("userId");
        Map<String,Object>map1=new HashMap<>();
        if(StringUtils.isNotBlank(teamIdString)&&StringUtils.isNotBlank(userId)){
            long userId1=Long.parseLong((String) map.get("userId"));
            long teamId = Long.parseLong((String) map.get("teamId"));
            map1.put("records",mailMapper.queryTimedMail(userId1,teamId,(page-1)*limit,limit));
            map1.put("total",mailMapper.queryTimedMailCount(userId1,teamId));
            return DataResult.success(map1);
        }
        if(StringUtils.isNotBlank(teamIdString)){
            long teamId = Long.parseLong((String) map.get("teamId"));
            map1.put("records",mailMapper.queryTimedTeam(teamId,(page-1)*limit,limit));
            map1.put("total",mailMapper.queryTimedTeamCount(teamId));
            return DataResult.success(map1);
        }else{
            long userId1=Long.parseLong((String) map.get("userId"));
            map1.put("records",mailMapper.queryTimedAccountEmail(userId1,(page-1)*limit,limit));
            map1.put("total",mailMapper.queryTimedAccountEmailCount(userId1));
            return DataResult.success(map1);
        }
    }
}
